﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Export2IMG
{
    public partial class GenerateForm : Form
    {
        string rootPath =
                   Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));

        public GenerateForm()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (checkNumber(txtNamSinh.Text))
            {
                txtTenMau.Text = getFullName();
                txtNgayThangSinh.Text = getRandomDateTime(int.Parse(txtNamSinh.Text));
            }
            else
            {
                MessageBox.Show("Nhap nam sinh!");
            }
        }
        //========================================================================================
        //Check number
        private bool checkNumber(string data)
        {
            try
            {
                int result = int.Parse(data);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
		
		//========================================================================================
        //Random date and month
        private String getRandomDateTime(int year)
        {
            Random rd = new Random();
            int date = 0;
            int month = 0;
            do{
                date = rd.Next(1, 31);
                month = rd.Next(1, 12);
            }
            while(!checkDate(date + "/" + month + "/" + year));
            
            return date + "/" + month;
        }
        public bool checkDate(String strDateTime)
        {
            try
            {
                String[] arrDateTime = strDateTime.Split('/');
                int date = int.Parse(arrDateTime[0]);
                int month = int.Parse(arrDateTime[1]);
                int year = int.Parse(arrDateTime[2]);

                if ((date < 1) || (date > 31) ||
                   (month < 0) || (month > 11) ||
                   (year < 1))
                {
                    return false;
                }

                if ((date == 31) &&
                        ((month == 4) || (month == 6) ||
                         (month == 9) || (month == 11)))
                {
                    return false;
                }

                if (month == 2)
                {
                    if ((year % 4 == 0) && (date >= 30))
                    {
                        return false;
                    }
                    else if ((year % 4 != 0) && (date >= 29))
                    {
                        return false;
                    }
                }

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //========================================================================================
        //Random full name
        private String getFullName()
        {
            //Get first name
            String[] arrFirstName = getDataFile(rootPath + "/data/Ho.txt");
            //Create array center Name and last name
            String[] arrCenterName;
            String[] arrLastName;
            if (rdNam.Checked)
            {
                //Get center name
                arrCenterName = getDataFile(rootPath + "/data/DemNam.txt");
                //Get last name
                arrLastName = getDataFile(rootPath + "/data/TenNam.txt");
            }
            else
            {
                //Get center name
                arrCenterName = getDataFile(rootPath + "/data/DemNu.txt");
                //Get last name
                arrLastName = getDataFile(rootPath + "/data/TenNu.txt");
            }

            //Random full name
            Random rd = new Random();
            return arrFirstName[rd.Next(0, arrFirstName.Length - 1)].Trim()
                    + " " + arrCenterName[rd.Next(0, arrCenterName.Length - 1)].Trim()
                    + " " + arrLastName[rd.Next(0, arrLastName.Length - 1)].Trim();
        }
        
        //Read file text
        private String[] getDataFile(String path)
        {
            //Pass the file path and file name to the StreamReader constructor
            StreamReader sr = new StreamReader(path, Encoding.Unicode);

            //Array string result
            String[] result = new String[50];
            
            //Read the first line of text
            int i = 0;
            result[i++] = sr.ReadLine();

            //Continue to read until you reach end of file
            while (result[i-1] != null)
            {
                //write to arraylist
                result[i++] = sr.ReadLine();
            }
            
            //close the file
            sr.Close();

            String[] arrReturn = new String[--i];
            Array.Copy(result, arrReturn, i);
            return arrReturn;
        }

        private void GenerateForm_Load(object sender, EventArgs e)
        {

        }

    }
}
